/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : MqcpDeviceInfoReport.c
  * @brief          : MqcpDeviceInfoReport
  ******************************************************************************
  * @attention
  * 本文件用于  用户信息上报
  * @20210414:文件模板第一次生成
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */


#include "MqcpRegWrite.h"

static MqcpDataDef *MqcpRegWriteReqProcess(MqcpMessageDef *Message);
static MqcpDataDef *MqcpRegWriteRespProcess(MqcpMessageDef *Message);


void RegWriteInit(void)//用户信息上报初始化
{
	MqcpRouterDef routeReq = 
	{
		1,
		RegWriteReq,
		MqcpRegWriteReqProcess
	};
	while(addRouter(routeReq));
	
	MqcpRouterDef routeResp = 
	{
		1,
		RegWriteResp,
		MqcpRegWriteRespProcess
	};
	while(addRouter(routeResp));
}


MqcpDataDef *MqcpRegWriteReqProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	MqcpData->isNeedResp = 1;
	MqcpData->Size = 0;
	
	return MqcpData;
}

MqcpDataDef *MqcpRegWriteRespProcess(MqcpMessageDef *Message)
{
	MqcpDataDef *MqcpData = rt_malloc(sizeof(MqcpDataDef));
	
	if(NULL == MqcpData)
	{
		return NULL;
	}
	
	MqcpData->isNeedResp = 0;
	MqcpData->Size = 0;
	uint16_t MessageSize = 0;//接收的data数
	uint16_t MqcpSize = 0;//发送的data数
	
	uint16_t RegAddr = 0;
	uint16_t RegSize = 0;//读取的数量
	RegSize = (Message->Data[1]<<8)|Message->Data[0];
	
	
	rt_memcpy(MqcpData->MqcpMessage.MsgId,Message->MsgId,12);MqcpData->Size = MqcpData->Size +12;
	MqcpData->MqcpMessage.Sync = MqcpSyncFalse;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Code = MqcpCodeOK;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.Ver = MQCP_USE_VER;MqcpData->Size = MqcpData->Size + 1;
	MqcpData->MqcpMessage.DevID[3] = MqcpDevice.DevId>>24;
	MqcpData->MqcpMessage.DevID[2] = MqcpDevice.DevId>>16;
	MqcpData->MqcpMessage.DevID[1] = MqcpDevice.DevId>>8;
	MqcpData->MqcpMessage.DevID[0] = MqcpDevice.DevId&0xff;MqcpData->Size = MqcpData->Size + 4;
	MqcpData->MqcpMessage.Cmd = RegWriteResp;MqcpData->Size = MqcpData->Size + 1;
	
	MqcpData->MqcpMessage.Data[MqcpSize] 	= Message->Data[MessageSize];
	MqcpData->MqcpMessage.Data[MqcpSize+1] = Message->Data[MessageSize + 1];MqcpData->Size = MqcpData->Size + 2;MqcpSize = MqcpSize + 2;MessageSize = MessageSize + 2;
	
	for(uint16_t i = 0; i < RegSize; i++)
	{
		if((MqcpInt32 == Message->Data[MessageSize])||(MqcpUint32 == Message->Data[MessageSize])||(MqcpFloat32 == Message->Data[MessageSize]))
		{
			MqcpData->MqcpMessage.Data[MqcpSize] = Message->Data[MessageSize];MqcpData->Size = MqcpData->Size + 1;MessageSize = MessageSize + 1;MqcpSize = MqcpSize + 1;
			
			RegAddr = ((Message->Data[MessageSize+1]<<8)|Message->Data[MessageSize]) - 1;
			
			MqcpData->MqcpMessage.Data[MqcpSize] = Message->Data[MessageSize];
			MqcpData->MqcpMessage.Data[MqcpSize + 1] = Message->Data[MessageSize + 1];MqcpData->Size = MqcpData->Size + 2;MessageSize = MessageSize + 2;MqcpSize = MqcpSize + 2;
			
			WriteValueFlash(RegAddr,&(Message->Data[MessageSize]),2);
			MqcpData->MqcpMessage.Data[MqcpSize + 1] = AddrGetValue(RegAddr)>>8;
			MqcpData->MqcpMessage.Data[MqcpSize + 0] = AddrGetValue(RegAddr)&0xff;
			MqcpData->MqcpMessage.Data[MqcpSize + 3] = AddrGetValue(RegAddr + 1)>>8;
			MqcpData->MqcpMessage.Data[MqcpSize + 2] = AddrGetValue(RegAddr + 1)&0xff;MqcpData->Size = MqcpData->Size + 4;MqcpSize = MqcpSize + 4;
			
			for(uint16_t i = 0; i < Data4Size; i++)
			{
				if(RegAddr == Data4Byte[i].Address - 1)
				{
					Data4Byte[i].Data[0] = AddrGetValue(Data4Byte[i].Address-1);
					Data4Byte[i].Data[1] = AddrGetValue(Data4Byte[i].Address-1 + 1);
					break;
				}
			}
		}
		else if(MqcpBool == Message->Data[MessageSize])
		{
			MqcpData->MqcpMessage.Data[MqcpSize] = Message->Data[MessageSize];MqcpData->Size = MqcpData->Size + 1;MqcpSize = MqcpSize + 1;MessageSize = MessageSize + 1;
			
			RegAddr = ((Message->Data[MessageSize+1]<<8)|Message->Data[MessageSize]) - 1;
			MqcpData->MqcpMessage.Data[MqcpSize] = Message->Data[MessageSize];
			MqcpData->MqcpMessage.Data[MqcpSize + 1] = Message->Data[MessageSize + 1];MqcpData->Size = MqcpData->Size + 2;MqcpSize = MqcpSize + 2;MessageSize = MessageSize + 2;
			WriteValueFlash(RegAddr,&(Message->Data[MessageSize]),0);
			MqcpData->MqcpMessage.Data[MqcpSize + 1] = AddrGetValue(RegAddr)>>8;
			MqcpData->MqcpMessage.Data[MqcpSize] = AddrGetValue(RegAddr)&0xff;MqcpData->Size = MqcpData->Size + 2;MqcpSize = MqcpSize + 2;MessageSize = MessageSize + 2;
			for(uint16_t i = 0; i < Data2Size; i++)
			{
				if(RegAddr == Data2Byte[i].Address - 1)
				{
					Data2Byte[i].Data = AddrGetValue(Data2Byte[i].Address-1);
					break;
				}
			}
		}
		else
		{
			MqcpData->MqcpMessage.Data[MqcpSize] = Message->Data[MessageSize];MqcpData->Size = MqcpData->Size + 1;MqcpSize = MqcpSize + 1;MessageSize = MessageSize + 1;
			
			RegAddr = ((Message->Data[MessageSize+1]<<8)|Message->Data[MessageSize]) - 1;
			MqcpData->MqcpMessage.Data[MqcpSize] = Message->Data[MessageSize];
			MqcpData->MqcpMessage.Data[MqcpSize + 1] = Message->Data[MessageSize + 1];MqcpData->Size = MqcpData->Size + 2;MqcpSize = MqcpSize + 2;MessageSize = MessageSize + 2;
			WriteValueFlash(RegAddr,&(Message->Data[MessageSize]),1);
			MqcpData->MqcpMessage.Data[MqcpSize + 1] = AddrGetValue(RegAddr)>>8;
			MqcpData->MqcpMessage.Data[MqcpSize] = AddrGetValue(RegAddr)&0xff;MqcpData->Size = MqcpData->Size + 2;MqcpSize = MqcpSize + 2;MessageSize = MessageSize + 2;
			for(uint16_t i = 0; i < Data2Size; i++)
			{
				if(RegAddr == Data2Byte[i].Address - 1)
				{
					Data2Byte[i].Data = AddrGetValue(Data2Byte[i].Address-1);
					break;
				}
			}
		}
	}
	
	MqcpData->MqcpMessage.Len[1] = MqcpSize>>8;
	MqcpData->MqcpMessage.Len[0] = MqcpSize&0xff;MqcpData->Size = MqcpData->Size + 2;
	
	MqcpData->MqcpMessage.Data[MqcpSize] = Chack_Xor((uint8_t*)(&(MqcpData->MqcpMessage)),MqcpData->Size);MqcpData->Size = MqcpData->Size + 1;
	
	return MqcpData;
}

/************************ (C) COPYRIGHT CunJu *****END OF FILE****/
